﻿using System;
using System.Net.Mime;
using System.Text;
using System.Web;
using System.Web.Mvc;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Enums;
using MiniWeb.Task;
using System.Linq;
using MiniWeb.Web.Controllers.Code;
using MiniWeb.Web.Controllers.Web.web.ViewModels;
using WFramework;
using WFramework.Common;
using WFramework.MvcPager;
using WFramework.Utility;

namespace MiniWeb.Web.Controllers.Web.Shopping
{
    public class shoppingController : BaseController
    {
        private readonly ShopCartTask _shopCartTask = new ShopCartTask();
        private readonly ProductsInfoTask _productsInfoTask = new ProductsInfoTask();
        private readonly AccountsInfoTask _accountsInfoTask = new AccountsInfoTask();
        private readonly StmpConfigTask _stmpConfigTask = new StmpConfigTask();
        private readonly OrderAddressTask _orderAddressTask = new OrderAddressTask();
        public readonly OrdersTask _OrdersTask = new OrdersTask();

        public ActionResult Cart()
        {


            if (!WebFormsAuthService.IsSignedIn())
            {
                return Json(new { ret = 0, msg = "请先登陆" });
            }

            var account = WebFormsAuthService.GetCurrentAccount();
            var model = _shopCartTask.GetCartList(account);
            return View(model);
        }
        [HttpPost]
        public ActionResult AddCart(int pid, int num)
        {
            if (!WebFormsAuthService.IsSignedIn())
            {
                return Json(new { ret = 0, msg = "请先登陆" });
            }

            var username = WebFormsAuthService.GetCurrentAccount();
            var accountinfo = _accountsInfoTask.GetByAccount(username);

            string msg;
            bool hasadd = _shopCartTask.AddCart(accountinfo.Account, pid, num, out msg);
            if (!hasadd)
            {
                return Json(new { ret = 0, msg = "添加失败，联系管理员！" });
            }
            return Json(new { ret = 1, msg = "添加成功!" });
        }
        [HttpPost]
        public ActionResult ChangeCart(int id, int num)
        {
            if (!WebFormsAuthService.IsSignedIn())
            {
                return Json(new { ret = 0, msg = "请先登陆" });
            }

            var model = _shopCartTask.GetById(id);
            if (model == null)
            {
                return Json(new { ret = 0, msg = "改动失败！" });
            }
            model.Num = num;

            _shopCartTask.Update(model);
            return Json(new { ret = 1 });

        }

        public ActionResult Delete(int sid)
        {
            if (!WebFormsAuthService.IsSignedIn())
            {
                return Json(new { ret = 0, msg = "请先登陆" });
            }

            _shopCartTask.Delete(WebFormsAuthService.GetCurrentAccount(), sid);

            return RedirectToAction("Cart");
        }

        public ActionResult Confirm()
        {
            if (!WebFormsAuthService.IsSignedIn())
            {
                return AlertMsg("请先登陆", "/home/login");
            }

            var accounts = WebFormsAuthService.GetCurrentAccount();
            var address = _orderAddressTask.GetDefault(accounts);
            if (address == null)
            {
                return AlertMsg("请先设置默认收货地址", "/usercenter/address");
            }
            ViewData["address"] = address;
            ViewData["stmplist"] = _stmpConfigTask.GetAll(null, null, null);
            ViewData["cartlist"] = _shopCartTask.GetCartList(accounts);
            ViewData["address"] = _orderAddressTask.GetDefault(accounts);

            return View();
        }
        [HttpPost]
        public ActionResult saveorder(int stimpid, int paytype)
        {
          
            if (!WebFormsAuthService.IsSignedIn())
            {
                return Json(new { ret = 0, msg = "请先登陆" });
            }
            var account = WebFormsAuthService.GetCurrentAccount();
            string msg = string.Empty;
            string order = string.Empty;
            bool issuccess = _OrdersTask.CreateOrder(account, out msg, out order, stimpid, paytype);
            if (issuccess)
            {
                return RedirectToAction("Success", new { code = order });
            }
            else
            {
                return Json(new { ret = 0, msg = "提交失败" });
            }

        }
        public ActionResult Success(string code)
        {
            if (!WebFormsAuthService.IsSignedIn())
            {
                return AlertMsg("请先登陆", "/home/login");
            }

            if (string.IsNullOrEmpty(code))
            {
                return new EmptyResult();
            }
            var model = _OrdersTask.GetByCode(code);
            if (model == null)
            {
                return new EmptyResult();
            }
            if (model.Account != WebFormsAuthService.GetCurrentAccount())
            {
                return new EmptyResult();
            }
            if (model.State != (int)VoucherStateEnum.待充值)
            {
                return new EmptyResult();
            }

            return View(model);
        }

    }
}
